package com.example.satu.quizcreator.quiz_chooser;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.satu.quizcreator.R;
import com.example.satu.quizcreator.quiz_chooser.interfaces.onQuizDownloadClickListener;

/**
 * Created by Satu on 2014-10-30.
 */

public class QuizCloudAdapter extends ArrayAdapter<String> {
    private final LayoutInflater mInflater;
    public QuizCloudAdapter(Context context) {
        super(context, R.layout.quiz_cloud_item);
        mInflater = LayoutInflater.from(getContext());
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null) {
            convertView = mInflater.inflate(R.layout.quiz_cloud_item, null);
            holder = new ViewHolder();
            holder.quiz = (TextView) convertView.findViewById(R.id.simple_quiz_item);
            holder.download = (Button) convertView.findViewById(R.id.download_quiz);
            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        //final ParseObject parseObject = getItem(position);
        String item = getItem(position);
        holder.download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
/*                Quiz quiz = null;
                DropboxAPI<AndroidAuthSession> dropboxAPI = null;
                CloudManager cloudManager = null;
                if (getContext() instanceof GetDbAPI)
                    dropboxAPI = ((GetDbAPI) getContext()).getDropBoxApi();
                if(dropboxAPI != null) {
                    cloudManager = new CloudManager(dropboxAPI,getContext());
                }
                quiz = cloudManager.download(getItem(position));*/
                if(getContext() instanceof onQuizDownloadClickListener)
                    ((onQuizDownloadClickListener) getContext()).onDownloadClick(getItem(position), QuizRunnerActivity.DOWNLOAD_QUIZ);
/*                try {
                    Quiz quiz = ParcelableUtil.unmarshall(parseObject.getParseFile("quiz").getData(), Quiz.CREATOR);
                    if(getContext() instanceof onQuizDownloadClickListener)
                        ((onQuizDownloadClickListener) getContext()).onDownloadClick(quiz);
                } catch (ParseException e) {
                    e.printStackTrace();
                }*/
            }
        });


       // Quiz quiz = getItem(position);
        //String quizName = getItem(position);
        holder.quiz.setText(item);

        return convertView;
    }
/*    public String getQuiz(int position) {
        return getItem(position);*/
/*        Quiz quiz = null;
        DropboxAPI<AndroidAuthSession> dropboxAPI = null;
        CloudManager cloudManager = null;
        if (getContext() instanceof GetDbAPI)
            dropboxAPI = ((GetDbAPI) getContext()).getDropBoxApi();
        if(dropboxAPI != null) {
            cloudManager = new CloudManager(dropboxAPI,getContext());
        }
        quiz = cloudManager.download(getItem(position));
        return quiz;*/
/*        BuiltObject  builtObject = getItem(position);
        BuiltFile quiz_file = builtObject.getBuiltFile("quiz_file");*/
/*        quiz_file.downloadFile((Activity) getContext(),quiz_file,new BuiltFileDownloadCallback() {
*//*            @Override
            public void onSuccess(File file) {
                try {
                   // byte[] bytes = Files.toByteArray(file);
*//**//*                    Serializer serializer = new Serializer();
                    quiz[0] = (Quiz) serializer.deserialize(bytes);*//**//*

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(BuiltError builtError) {
                Toast.makeText(getContext(),"nie pobrano",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAlways() {

            }
        });*/
        //  final ParseObject parseObject = getItem(position);
     //   return ParcelableUtil.unmarshall(parseObject.getParseFile("quiz").getData(), Quiz.CREATOR);
 //   }


    private static class ViewHolder{
        public TextView quiz;
        public Button download;
    }
}
